package com.portal.cwegportal.controller;
import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;

import com.portal.cwegportal.input.maillogininfo.MailLoginInfoInput;
import com.portal.cwegportal.input.usermailaccount.EditUserMailAccountInput;
import com.portal.cwegportal.model.api.Result;
import com.portal.cwegportal.output.mail.GetMailInfoOutput;
import com.portal.cwegportal.service.MailInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/services/portal/mailinfo")
@Api(description ="邮箱API")
public class MailInfoController {
    @Autowired
    MailInfoService mailInfoService;

    @ApiOperation(value = "查询未读邮件个数", notes = "查询未读邮件个数", produces = "application/json")
    @PostMapping("/getNoReadMail")
    public ResponseEntity<JsonResult> getNoReadMail() throws Exception {
        BusinessLogger.Info( LoggerGenre.Add,"查询未读邮件个数","");
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();

        try {
            r.setResult(mailInfoService.getNoReadCounts());
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(200).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "得到当前用户邮箱", notes = "得到当前用户邮箱", produces = "application/json")
    @PostMapping("/getUserMail")
    public ResponseEntity<JsonResult> getUserMail() throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();

        try {
            Result mail=mailInfoService.getUserEmailInfo();
            mail.setEmailPassword("");
            r.setResult(mail);
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(200).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "测试登陆", notes = "测试登陆", produces = "application/json")
    @PostMapping("/testLogin")
    public ResponseEntity<JsonResult> testLogin(@RequestBody MailLoginInfoInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"测试登陆",input);
        JsonResult r = new JsonResult();

        try {
            mailInfoService.getSessid(input.getItem().getMail(),input.getItem().getMailPassword());
            r.setResult(true);
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(200).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @Autowired
    com.portal.cwegportal.service.UserMailAccountService userMailAccountService;
    @ApiOperation(value = "重置邮箱密码", notes = "重置邮箱密码",produces = "application/json")
    @PostMapping("/resetMailPassword")
    public ResponseEntity<JsonResult> resetMailPassword(@RequestBody MailLoginInfoInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"重置邮箱密码",input);
        JsonResult r = new JsonResult();
        try {
            mailInfoService.ResetMailPassword(input);;
            r.setResult(null);
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(200).body(r);
        }
        return ResponseEntity.ok(r);
    }
    @ApiOperation(value = "绑定邮箱密码", notes = "绑定邮箱密码",produces = "application/json")
    @PostMapping("/bindMailPassword")
    public ResponseEntity<JsonResult> bindMailPassword(@RequestBody MailLoginInfoInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"绑定邮箱密码",input);
        JsonResult r = new JsonResult();
        try {
            mailInfoService.BindMailPassword(input);;
            r.setResult(null);
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(200).body(r);
        }
        return ResponseEntity.ok(r);
    }
    @ApiOperation(value = "发送验证码", notes = "发送验证码",produces = "application/json")
    @PostMapping("/sendVerificationCode")
    public ResponseEntity<JsonResult> sendVerificationCode() throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"发送验证码","");
        JsonResult r = new JsonResult();
        try {
            mailInfoService.sendVerificationCode();
            r.setResult(null);
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(200).body(r);
        }
        return ResponseEntity.ok(r);
    }

}
